/**
 * @desc:用户注册页面
 * @author: zhoujin
  * @date: 2019-08-01
  * @update: 2019-08-01
  * @version: 1.0
  * @copyright: 2019-08-01
  * @remark:
 */
 <template>
   <div class="register">
    <div class="register-layout">
      <div class="form-container">
        <el-form ref="form" :model="form" :rules="rules" >
        <div class="title">我的后台管理系统</div>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input v-model="form.rePassword" placeholder="请输入密码" ></el-input>
        </el-form-item>
        <el-form-item label="选择身份" prop="identity">
          <el-select v-model="form.identity">
            <el-option label="管理员" value="manager"></el-option>
            <el-option label="游客" value="visitor"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="submit_btn" @click="onRegister('form')">注册</el-button>
        </el-form-item>
        </el-form>
      </div>
    </div>
   </div>
 </template>
 
 <script>
 import { register } from '@/api/user';
 import { isPassWord } from '@/utils/validate';
 export default {
  name:'register',
  data() {
    return {
      form: {
        name:"周瑾3",
        email:"zhoujin3@163.com",
        password:"323456",
        rePassword:"323456",
        identity:"manager"

      },
      rules:{
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        rePassword:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        identity:[
          { required: true, message: '请选择身份', trigger: 'change' }
        ]
      }
      
    }
  },
  methods: {
    onRegister(form) {
      this.$refs[form].validate((valid) => {
        if(valid) {
          let params = this.form;
          const res = register(params)
          res.then(res => {
            if(res.status === 200) {
              this.$message({
                message: '恭喜你，注册成功',
                type: 'success'
              })
            }
            this.$router.push({path:'/login'})
          })
          .catch(err => {
            console.log('err注册-catch-业务页面=======================', err);
          })
        }else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }





 }
 </script>
 
 <style lang="less" scoped>
  .register {
    height: 100vh;
    display: flex;
    .register-layout {
      flex:1;
      display: flex;
      justify-content: center;
      align-items: center;
      // TODO-bug: form内容超过容器
      .form-container {
        width: 400px;
        height: 400px;
      }
    }
  }
 </style>
 <style lang="less">
 .register {
  .register-layout {
    .form-container {
        .el-form {
          .el-form-item {
            margin-bottom: 5px !important;
          }
        }
        .submit_btn {
          width: 100%;
        }
      }
    }
  }
 </style>